home *** CD-ROM | disk | FTP | other *** search
- #!/usr/sbin/perl
- #
- # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.
- #
- # A simple mailer that supports webjumper attachment and non-ascii content.
- # It is intended to be used within custreg and memphis.
- #
- # command line:
- #
- # mailer.pl [-subject subject]
- # [-from from-string] [-cc cc-addr]
- # [-attach type:filepath] dest < content
- #
-
- #
- # constants
- #
- $flagExclusiveLock = 2;
- $flagUnlock = 8;
-
- #
- # customizable variables
- #
- $mailerFile = "/usr/tmp/custreg_mailer_$$.txt";
-
- #
- # a list of charsets, the default will be iso-8859-1
- #
- %CHARSETS = (
- 'ja_JP', 'iso-2022-jp',
- 'ja_JP.EUC', 'iso-2022-jp',
- 'ko_KR', 'iso-2022-kr',
- 'ko_KR.EUC', 'iso-2022-kr',
- 'en_US', 'us-ascii',
- 'C', 'us-ascii',
- 'de', 'iso-8859-1',
- 'fr', 'iso-8859-1',
- 'fr_CA', 'iso-8859-1',
- 'es', 'iso-8859-1',
- 'es_MX', 'iso-8859-1',
- );
-
- #
- # lang, if it's a multi-byte language
- #
- %MULTIBYTELANG = (
- 'ja_JP', 1,
- 'ja_JP.EUC', 1,
- 'ko_KR', 1,
- 'ko_KR.EUC', 1,
- );
- #
- # $INPUTS associative array stores input arguments
- #
- %INPUTS = ();
-
- #
- # start of mailer
- #
-
- if ($#ARGV < 0) {
- print <<End_Usage;
- Usage:
- mailer.pl flags dest < content
-
- where flags can be:
- [-subject subject]
- [-from from-string]
- [-cc cc-addr]
- [-attach type:filepath]
- End_Usage
-
- exit(1);
- }
- else {
- #
- # obtain cmd line arguments
- #
- &getCmdLineArgs(*INPUTS);
-
- &buildMailFile(*INPUTS);
- &sendMailFile();
- }
-
-
- #
- # obtain cmd line arguments
- #
- sub getCmdLineArgs
- {
- #
- # loop thru the cmdline options
- #
- $i = 0;
- while($i <= $#ARGV) {
- if ($ARGV[$i] eq "-subject") {
- $i++;
- $INPUTS{'subject'} = $ARGV[$i];
- }
- elsif ($ARGV[$i] eq "-from") {
- $i++;
- $INPUTS{'from'} = $ARGV[$i];
- }
- elsif ($ARGV[$i] eq "-cc") {
- $i++;
- $INPUTS{'cc'} = $ARGV[$i];
- }
- elsif ($ARGV[$i] eq "-attach") {
- $i++;
- $INPUTS{'attach'} = $ARGV[$i];
- }
- else {
- #
- # dest address.
- #
- $INPUTS{'dest'} = $ARGV[$i];
- }
- $i++;
- }
- }
-
- #
- # build the mailer file with proper MIME headers
- #
- sub buildMailFile
- {
- open(FILE, "> $mailerFile") || die "Cannot open: $mailerfile: $!\n";
-
- flock(FILE, $flagExclusiveLock);
-
- #
- # get host name
- #
- $host = `/usr/bsd/hostname`;
- chop $host;
- local(@a) = gethostbyname($host);
- $host = $a[0] unless ! @a;
-
- #
- # print the mail header
- #
- if ($INPUTS{'from'} ne "") {
- if ($INPUTS{'from'} =~ /<\w+>/) {
- print FILE "From: $INPUTS{'from'}\n";
- }
- else {
- $login = getlogin || (getpwuid($<))[0] || "nobody";
- if ($INPUTS{'to'} =~ /\@/) {
- print FILE "From: $INPUTS{'from'} <$login@$host>\n";
- }
- else {
- print FILE "From: $INPUTS{'from'} <$login>\n";
- }
- }
- }
-
- print FILE "X-Mailer: mailer.pl (SGI/CustReg/mailer)\n";
- print FILE "To: $INPUTS{'dest'}\n";
- print FILE "Subject: $INPUTS{'subject'}\n";
- print FILE "Cc: $cc\n";
-
- #
- # print the MIME header
- #
- print FILE "Mime-Version: 1.0\n";
-
- $boundary = "PART-BOUNDARY=.$$.$host";
-
- if ($INPUTS{'attach'}) {
- print FILE "Content-Type: multipart/mixed;\n";
- print FILE " boundary=\"$boundary\"\n\n";
-
- #
- # separator
- #
- print FILE "--\n--$boundary\n";
- }
-
- #
- # email body
- #
- $lang = $ENV{'LANG'};
- $charset = $CHARSETS{$lang};
- if ($charset eq "") {
- $charset = "us-ascii";
- }
- $content = "";
- while(<STDIN>) { $content .= $_; }
-
- print FILE "Content-Type: text/plain; charset=$charset\n";
-
- #
- # No encoding - Tony Tam 11/21/96
- #
- print FILE "\n";
- print FILE $content;
-
- ############################################################
- # TAKE OUT THIS SECTION BECAUSE MEMPHIS ALREADY DOES
- # ENCODING - Tony Tam 11/21/96
- #
- # if (($lang eq "") || ($lang eq "en_US") || ($lang eq "C")) {
- # print FILE "\n";
- # print FILE $content;
- # }
- # elsif ($MULTIBYTELANG{$lang}) {
- # print FILE "Content-Transter-Encoding: base64\n\n";
- # print FILE &encodeBase64($content);
- # }
- # else {
- # print FILE "Content-Transter-Encoding: quote-printable\n\n";
- # print FILE &encodeQuotedPrint($content);
- # }
- #######################################################
-
- print FILE "\n";
-
- #
- # attachment. it's mostly webjumper so we use charset=us-ascii
- #
- if ($INPUTS{'attach'}) {
- ($atype, $afilepath) = split(/:/, $INPUTS{'attach'});
- open (AFILE, $afilepath) || die "Cannot open $afilepath: $!\n";
-
- print FILE "\n--$boundary\n";
- print FILE "Content-Type: $atype; charset=us-ascii\n\n";
-
- while (<AFILE>) {
- print FILE;
- }
-
- #
- # separator
- #
- print FILE "\n--$boundary--\n\n";
- }
-
- flock(FILE, $flagUnlock);
- close(FILE);
- }
-
- sub sendMailFile
- {
- open (MAIL, "| /usr/lib/sendmail -t -n") || die "Cannot do sendmail: $!\n";
-
- open (FILE, $mailerFile) || die "Cannot open $mailerFile: $!\n";
-
- while (<FILE>) {
- print MAIL;
- }
- close (FILE);
- close (MAIL);
-
- unlink $mailerFile;
- }
-
- #
- # encodeBase64 is backward ported from perl5's Base64::encode
- # because perl5 is not bundled to 6.3, as of 7/28/96.
- #
- sub encodeBase64
- {
- local ($res) = "";
- local ($src) = shift;
- local ($eol) = shift;
- $eol = "\n" unless defined $eol;
- $* = 1; # enalble multi-line patterns
- while ($src =~ /(.{1,45})/g) {
- $res .= substr(pack('u', $1), 1);
- chop($res);
- }
- $* = 0; # disable multi-line patterns
-
- $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
- # fix padding at the end
- local ($padding) = (3 - length($_[0]) % 3) % 3;
- $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
- # break encoded string into lines of no more than 76 characters each
- if (length $eol) {
- $res =~ s/(.{1,76})/$1$eol/g;
- }
-
- $res;
- }
-
- #
- # encodeQuotedPrint is backward ported from perl5's QuotedPrint::encode
- # because perl5 is not bundled to 6.3, as of 7/28/96.
- #
- sub encodeQuotedPrint
- {
- local ($res) = shift;
- $res =~ s/([^ \t\n!-<>-~])/sprintf("=%02X", ord($1))/eg; # rule #2,#3
- $res =~ s/([ \t]+)$/
- join('', map { sprintf("=%02X", ord($_)) } split('', $1)
- )/eg; # rule #3 (encode whitespace at eol)
-
- # rule #5 (lines must be shorter than 76 chars, but we are not allowed
- # to break =XX escapes. This makes things complicated :-( )
- local ($brokenlines) = "";
- $brokenlines .= "$1=\n" while $res =~ s/^(.{74}([^=]{2})?)//;
- # unnessesary to make a break at the last char
- $brokenlines =~ s/=\n$// unless length $res;
-
- "$brokenlines$res";
- }
-
-